package leetcode.median;

import leetcode.base.ListNode;

/**
 * ClassName: $19_RemoveNthFromEnd
 * Description:
 * date: 2021-12-09 13:23
 *
 * @author liyifan
 */
public class $19_RemoveNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = findKthFromEnd(dummy, n + 1);
        pre.next = pre.next.next;
        return dummy.next;
    }

    ListNode findKthFromEnd(ListNode head, int n){
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0;i < n;i++){
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
